package com.happok.onlineclass.usermanagement.setting.controller;

import com.alibaba.fastjson.JSON;
import com.happok.onlineclass.usermanagement.setting.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/20 16:46
 */
@AutoConfigureMockMvc
public class SettingCommon {

    public static String UPDATE_EMAIL_SETTING_URL = "/user-management/email/v1";
    public static String UPDATE_SMS_SETTING_URL = "/user-management/sms/v1";
    public static String UPDATE_SMS_ENABLE_URL = "/user-management/sms/status/v1";
    public static String OPEN_USER_REGIST_URL = "/user-management/verify-code/setting/user-regist/status/v1";
    public static String UPDATE_USER_REGIST_URL = "/user-management/verify-code/setting/user-regist/v1";
    public static String UPDATE_USER_LOGIN_URL = "/user-management/verify-code/setting/user-login/v1";
    public static String UPDATE_FORGET_PASSWORD_URL = "/user-management/verify-code/setting/forget-password/v1";
    public static String UPDATE_RESET_PASSWORD_URL = "/user-management/verify-code/setting/reset-password/v1";
    public static String UPDATE_MODIFY_ACCOUNT_URL = "/user-management/verify-code/setting/modify-account/v1";
    public static String UPDATE_ADD_USER_URL = "/user-management/verify-code/setting/add-user/v1";

    @Autowired
    private static MockMvc mockMvc;

    public static void setMockMvc(MockMvc mockMvc) {
        SettingCommon.mockMvc = mockMvc;
    }

    public static void updateEmailSetting(EmailSettingDTO emailSetting, Integer code) throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.put(UPDATE_EMAIL_SETTING_URL)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSON(emailSetting).toString()))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(code))
                .andReturn();
    }

    public static void updateSmsSetting(SmsSettingDTO smsSetting, Integer code) throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.put(UPDATE_SMS_SETTING_URL)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSON(smsSetting).toString()))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(code))
                .andReturn();
    }

    public static void updateFunctionSetting(UpdateVerifyDTO updateVerify, String url) throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.put(url)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSON(updateVerify).toString()))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
    }

    public static void openOrCloseUserRegister(UserRegisterStatusDTO userRegisterStatusDTO) throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.put(OPEN_USER_REGIST_URL)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSON(userRegisterStatusDTO).toString()))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
    }

    public static void smsEnable(SmsEnableDTO smsEnable) throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.put(UPDATE_SMS_ENABLE_URL)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSON(smsEnable).toString()))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
    }
}